<?php

require_once( 'flexical/admin/listing-response.php' );
require_once( 'flexical/admin/controller.php' );

class Flexical_Admin_Listing_Controller extends Flexical_Admin_Controller
{
	public function respond( $request )
	{
		$response = null;

		switch ( get_class( $request ) )
		{
			case 'Flexical_Admin_Show_Listings_Request':
				$response = $this->show_listings();
				break;

			case 'Flexical_Admin_Create_Listing_Request':
				$response = $this->create_listing( $request->listing, $request->cancel_selected );
				break;

			case 'Flexical_Admin_Update_Listing_Request':
				$response = $this->update_listing( $request->listing, $request->cancel_selected );
				break;

			case 'Flexical_Admin_Delete_Listing_Request':
				$response = $this->delete_listing( $request->listing, $request->cancel_selected );
				break;

			case 'Flexical_Admin_Edit_Listing_Request':
				$response = $this->edit_listing( $request->listing );
				break;

			case 'Flexical_Admin_Confirm_Delete_Listing_Request':
				$response = $this->confirm_delete_listing( $request->listing );
				break;

			case 'Flexical_Admin_Test_Listing_Request':
				$response = $this->test_listing( $request->listing );
				break;

			case 'Flexical_Admin_View_Listing_Request':
				$response = $this->view_listing( $request->listing );
				break;

			case 'Flexical_Admin_Delete_All_Listings_Request':
				$response = $this->delete_all_listings( $request->confirmed );
				break;

			default:
				throw new Exception( "Unsupported request class: " . get_class( $request ) );
		}

		return $response;
	}

	private function show_listings()
	{
		$response = new Flexical_Admin_Show_Listings_Response();
		$this->build_response( $response );

		return $response;
	}

	private function build_response( $response )
	{
		$response->feeds    = $this->get_dao()->read_feeds();
		$response->queries  = $this->get_dao()->read_queries();
		$response->formats  = $this->get_dao()->read_formats();
		$response->listings = $this->get_dao()->read_listings();
	}

	private function is_listing_valid( $listing )
	{
		$valid = true;

		if ( $listing->format_id < 1 )
		{
			$valid = false;
		}

		foreach ( $listing->feed_query_list as $fq_id => $feed_query )
		{
			if ( $feed_query->feed_id < 1 || $feed_query->query_id < 1 )
			{
				$valid = false;
			}
		}

		if ( sizeof( $listing->feed_query_list ) == 0 )
		{
			$valid = false;
		}

		return $valid;
	}

	private function create_listing( $listing, $cancel_selected )
	{
		$response = null;

		if ( $cancel_selected )
		{
			$response = $this->show_listings();
			$response->canceled = true;
		}

		else if ( $this->is_listing_valid( $listing ) )
		{
			$new_listing = $this->get_dao()->create_listing( $listing );

			$response = new Flexical_Admin_Create_Listing_Response();
			$response->listing = $new_listing;
		}

		else
		{
			$response = new Flexical_Admin_Listing_To_Update_Invalid_Response( $listing, 'create' );
		}

		$this->build_response( $response );

		return $response;
	}

	private function update_listing( $listing, $cancel_selected )
	{
		$response = null;

		if ( $cancel_selected )
		{
			$response = $this->show_listings();
			$response->canceled = true;
		}

		else if ( $this->is_listing_valid( $listing ) )
		{
			$this->get_dao()->update_listing( $listing );
			$response = new Flexical_Admin_Update_Listing_Response();
			$response->listing = $this->get_dao()->read_listing( $listing->id );
		}

		else
		{
			$response = new Flexical_Admin_Listing_To_Update_Invalid_Response( $listing, 'update' );
		}

		$this->build_response( $response );

		return $response;
	}

	private function edit_listing( $listing )
	{
		$response = new Flexical_Admin_Edit_Listing_Response();
		$response->listing = $this->get_dao()->read_listing( $listing->id );
		$response->operation = 'update';
		$this->build_response( $response );
		
		return $response;
	}

	private function confirm_delete_listing( $listing )
	{
		$response = new Flexical_Admin_Confirm_Delete_Listing_Response();
		$response->listing = $this->get_dao()->read_listing( $listing->id );
		$this->build_response( $response );

		return $response;
	}

	private function delete_listing( $listing, $cancel_selected )
	{
		$response = null;

		if ( $cancel_selected )
		{
			$response = $this->show_listings();
			$response->canceled = true;
		}

		else
		{
			$response = new Flexical_Admin_Delete_Listing_Response();
			$response->listing = $this->get_dao()->read_listing( $listing->id );
			$this->get_dao()->delete_listing( $listing->id );
		}

		$this->build_response( $response );
		return $response;
	}

	private function delete_all_listings( $confirmed )
	{
		$response = new Flexical_Admin_Delete_All_Listings_Response( $confirmed );

		if ( $confirmed )
		{
			foreach ( $this->get_dao()->read_listings() as $id => $listing )
			{
				$this->get_dao()->delete_listing( $id );
			}
		}

		$this->build_response( $response );

		return $response;
	}

	private function test_listing( $listing )
	{
		$listing = $this->get_dao()->read_listing( $listing->id );

		require_once( 'flexical/service/report-generator.php' );
		$generator = new Flexical_Service_Report_Generator();
		$report = $generator->generate( $listing );

		$response = new Flexical_Admin_Test_Listing_Response( $listing, $report );
		$this->build_response( $response );

		return $response;
	}
}

?>